# 接口测试
import os.path

import requests

from config1 import pict_path

# 百度
# url="http://www.baidu.com"
# 悟空
# url1="http://172.16.0.254:8080"
# r=requests.get(url)
# r=requests.get(url1)
#
# print(r,type(r))
# print(r.encoding)
# r.encoding='utf-8'
# print(r.text)

ipa="http://172.16.0.254:8080"
uri=f"{ipa}/login"
ur=os.path.join(ipa,uri)
# 设置变量
# 第一种
mydata={
    "username":"17513230010",
    "password":"123456"
}
# 第二种
# mydata="username=admin&password=123456"
# myheader={
#     "Content-Type":"application/x-www-form-urlencoded"
# }
# print(ur)
r=requests.post(uri,data=mydata)
# r=requests.post(uri,data=mydata,headers=myheader)
# print(r.json())

token=r.json()["Admin-Token"]
print(token)

# 新增日志
ipa="http://172.16.0.254:8080"
uri1=f"{ipa}/OaLog/addOrUpdate"
ur1=os.path.join(ipa,uri1)

# 文件上传
fileurl=f"{ipa}/file/upload"
fdata={"type":"img"}
ffile={"file":
        ('11.jpg',
        open(r'D:\work\pythonProject\Apitest\datas\11.jpg','rb'),'image/png')}
h={"Admin-Token":token}
t=requests.post(fileurl,data=fdata,files=ffile,headers=h)
# print(t.json())

bid=t.json()['batchId']

# 新增日志
myjson={
    "categoryId": "1",
    "content": "这里是今日内容~~",
    "tomorrow": "这里明日工作计划~",
    "question": "这里是今日遇到的问题",
    "batchId":bid,
    "sendUserIds": "3,4",
    "sendDeptIds": "",
    "customerIds": "",
    "contactsIds": "",
    "businessIds": "",
    "contractIds": ""
}
header1={
    "Content-Type":"application/json;charset=UTF-8",
    "Admin-Token":token
}
s=requests.post(uri1,json=myjson,headers=header1)
# print(s.json())

# 下载文件并保存
url=f"{ipa}/upload/20241223/112.jpg"
r=requests.get(url)
pname=os.path.join(pict_path,"python_001.jpg")
# 创建文件
with open(pname,"wb")as f:
    f.write(r.content)




